package io.renren.modules.workmanagement.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.workmanagement.constans.UserConstans;
import io.renren.modules.workmanagement.entity.WorkEntity;
import io.renren.modules.workmanagement.service.WorkService;
import io.renren.modules.workmanagement.vo.StudentWorkDetail;
import io.renren.modules.workmanagement.vo.WorkDetailsVO;
import io.renren.modules.workmanagement.vo.WorkResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 *
 *
 * @author yewei
 * @email sunlightcs@gmail.com
 * @date 2022-04-26 22:54:22
 */
@RestController
@RequestMapping("workmanagement/work")
public class WorkController extends AbstractController {
    @Autowired
    private WorkService workService;

    @RequestMapping("/getStudentWorkDetailList")
    public R getStudentWorkDetailList(@RequestParam Long workId){
        List<StudentWorkDetail> students = workService.getStudentWorkDetailList(workId);
        return R.ok().put("list", students);
    }

    @RequestMapping("/getWorkDetails")
    public R getWorkDetails(@RequestParam Long workId){

        WorkDetailsVO workDetailsVO = workService.getWorkDetails(workId,getUserId());
        return R.ok().put("work", workDetailsVO);
    }

    @RequestMapping("/getWorkResult")
    public R getWorkResult(@RequestParam Long workId,@RequestParam(required = false) Long userId){
        if(userId == null){
            userId = getUserId();
        }
        WorkResultVO workResult = workService.getWorkResult(workId,userId);
        return R.ok().put("result",workResult);
    }

    @RequestMapping("/submitWork")
    public R submitWork(@RequestParam Long workId){
        workService.submitWork(workId,getUserId());
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        params.put(UserConstans.USER_KEY,getUser());
        PageUtils page = workService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		WorkEntity work = workService.getById(id);

        return R.ok().put("work", work);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WorkEntity work){
		workService.save(work);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WorkEntity work){
		workService.updateById(work);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		workService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


}
